Array.prototype.Average = function () {
    var sum = 0;
    for (var i = 0; i < this.length; i++) {
        sum += this[i];
    }
    return sum / this.length
}


function getLinearRegression(array) {
    if (array == null) {
        return null;
    }
    if (array.length == 1) {
        return array;
    }
    var xNum = [];
    var yNum = [];
    for (var i = 0; i < array.length; i++) {
        var data = parseFloat(array[i]);
        if (isNaN(data)) {
            data = parseFloat(array[i].y);
        }
        yNum.push(data);
        xNum.push(parseFloat(i + 1))
    }
    var xa = xNum.Average();
    var ya = yNum.Average();
    var Lxx = 0;
    var Lxy = 0;
    for (i = 0; i < xNum.length; i++) {
        Lxx += ((xNum[i] - xa) * (xNum[i] - xa));
        Lxy += ((xNum[i] - xa) * (yNum[i] - ya));
    }
    var a = new Number();
    var b = new Number();
    b = Lxy / Lxx;
    a = ya - b * xa;
    var result = [];
    for (var i = 0; i < xNum.length; i++) {
        result.push(parseInt(parseFloat(b.toFixed(3) * xNum[i]) + parseFloat(a.toFixed(3))));
    }
    return result;
}

function predict(array) {

    if (array == null) {
        return null;
    }
    if (array.length == 1) {
        return array;
    }
    var xNum = [];
    var yNum = [];
    for (var i = 0; i < array.length; i++) {
        var data = parseFloat(array[i]);
        if (isNaN(data)) {
            data = parseFloat(array[i].y);
        }
        yNum.push(data);
        xNum.push(parseFloat(i + 1))
    }
    var xa = xNum.Average();
    var ya = yNum.Average();
    var Lxx = 0;
    var Lxy = 0;
    for (i = 0; i < xNum.length; i++) {
        Lxx += ((xNum[i] - xa) * (xNum[i] - xa));
        Lxy += ((xNum[i] - xa) * (yNum[i] - ya));
    }
    var a = new Number();
    var b = new Number();
    b = Lxy / Lxx;
    a = ya - b * xa;

    var x = array.length + 1;
    return parseInt(parseFloat(b.toFixed(3) * x) + parseFloat(a.toFixed(3)));
}





